<?php

/*
 * 精准营销
 */

class UserProduct_Inquire {
    /*
     * 数据表名
     */
    const TABLE_NAME = 'userproduct';

    /**
     * cache namaspace
     * 精准营销
     */
    const CACHE_NS = 'userproduct';

    /**
     * cache namaspace
     * 数据挖掘
     */
    const CACHE_COLLECT_NS = 'pid';

    /**
     * session user id namaspace
     */
    const SESSION_ID = 'UId';

    /**
     * session user inquire namaspace
     */
    const SESSION_INQUIRE_ID = 'userInquire';

    /**
     * 缓存实例
     *
     * @var Helper_Cache_Memcache
     */
    protected $_cacheInstance;
    /**
     * db实例
     *
     * @var Helper_Cache_Memcache
     */
    protected $_dbInstance;
    /**
     * 单例对象
     */
    public static $singletonObjects;

    /**
     * 获取单例
     */
    public static function getInstance() {
        $className = __CLASS__;
        if (!isset(self::$singletonObjects[$className])) {
            self::$singletonObjects[$className] = new self();
        }

        return self::$singletonObjects[$className];
    }

    /**
     * 构造器
     *
     * @param int $uId
     */
    protected function __construct() {

        $this->_cacheInstance = $this->_getCache();
        $this->_dbInstance = $this->_getMysql();
    }

    /*
     * 获取cache实例
     */

    public function _getCache() {

        return Common::getCache();
    }

    /*
     * 获取db实例
     */

    public function _getMysql() {

        return Common::getMysql();
    }

    /*
     * 添加Cache
     * 
     */

    public function addCache ( $arr ) {

        $this->_cacheInstance->add(self::CACHE_NS, $arr);
    }

    /*
     * 获取cache
     */

    public function getCacheValue($cache_ns) {

        return $this->_cacheInstance->get($cache_ns);
    }

    /*
     * 获取某产品查询次数
     */

    public function getCacheCount($pid) {

        $cache = $this->getCacheValue(self::CACHE_NS);
        return $cache[$_SESSION[self::SESSION_ID]][$pid]['Count'];
    }

    /*
     * 更新cache
     */

    public function updateCache($pid) {

        $count = $this->getCacheCount($pid);
        $cache = $this->getCacheValue(self::CACHE_NS);
        $cache[$_SESSION[self::SESSION_ID]][$pid]['Count'] = $count + 1;
        $cache[$_SESSION[self::SESSION_ID]][$pid]['Date'] = date(' Y-m-d H:m:s ', time());
        $this->_cacheInstance->set(self::CACHE_NS, $cache);
    }

    /*
     * PId
     * UId
     * 获取userproduct一条记录
     */

    public function getCount($pid) {

        $db = $this->_dbInstance;
        $where = array('UId' => $_SESSION[self::SESSION_ID], 'PId' => $pid);
        $arrs = $db->get(self::TABLE_NAME, $where);
        return $arrs['Count'];
    }

    /*
     * 更新数据库
     */

    public function updateUserProduct($pid) {

        $count = $this->getCount($pid);
        $count = $count + 1;
        $db = $this->_dbInstance;
        $where = array('UId' => $_SESSION[self::SESSION_ID], 'PId' => $pid);
        $value = array('Count' => $count, 'UpdateTime' => date(' Y-m-d H:m:s ', time()));
        $db->update(self::TABLE_NAME, $where, $value);
    }

    /*
     * 插入cache
     * 
     */

    public function insertCache($pid) {

        $cache = $this->getCacheValue(self::CACHE_NS);
        $cache [$_SESSION [self::SESSION_ID]] [$pid] = array('Count' => 1, 'Date' => date('Y-m-d H:m:s', time()));
        $this->_cacheInstance->set(self::CACHE_NS, $cache);
    }

    /*
     * 数据库添加记录
     */

    public function insertUserProduct($pid) {

        $db = $this->_dbInstance;
        $value = array('UId' => $_SESSION[self::SESSION_ID], 'PId' => $pid, 'Count' => 1, 'UpdateTime' => date(' Y-m-d H:m:s ', time()));
        $db->add(self::TABLE_NAME, $value);
    }

    /**
     * 用户登录根据 session 记入cache 和db
     * 
     */
    public function cookieSaveCacheMysql( $sessionInfo ){
        
        $cache = $this->getCacheValue(self::CACHE_NS);
        print_R ( $cache );
        echo "<br/>";
        //更新 mc 和 cache
        if( $cache ){
            
            
            
        }
        
        else{
            
            
        }
        //记入mc he db
        foreach( $sessionInfo as $key => $value ){
           
           
            
        }
        
        
    }
    
    
    
    
    
    
    /*
     * 用户查询记录记入cache和db
     * 精准营销
     */
    public function saveToCacheMysql($productinfo) {
        /*
         * 判断有无
         * cache
         */
        $cache = $this->getCacheValue(self::CACHE_NS);
        $pid = $productinfo ['PId'];
        
        if ($cache) {

            /*
             * 判断缓存中有无此用户ID记录
             * 有更新
             * 无添加
             */
            if (array_key_exists($_SESSION['UId'], $cache)) {

                //更新cache
                $arrayCachePId = $cache [$_SESSION ['UId']];

                if (array_key_exists($pid, $arrayCachePId)) {

                    $this->updateCache($pid);
                    $this->updateUserProduct($pid);
                } else {

                    $this->insertCache($pid);
                    $this->insertUserProduct($pid);
                }
            } else {

                //添加cache
                $this->insertCache($pid);
                $this->insertUserProduct($pid);
            }
        }

        /*
         * 无Mc，添加
         */ 
        
        else {
            
            $db = $this->_dbInstance;
            $row = $db->getList(self::TABLE_NAME, '1');

            if (count($row) > 0) {

                $cache = array();

                foreach ($row as $value) {

                    $cache [$value ['UId']] [$value ['PId']] = array('Count' => $value ['Count'], 'UpdateTime' => $value ['UpdateTime']);
                }
                
                /**
                 * 添加cache
                 */
                $this->addCache($cache) ;
                
                /*
                 * 判断缓存中有无此用户ID记录
                 * 有更新
                 * 无添加
                 */
                if (array_key_exists($_SESSION['UId'], $cache)) {

                    //更新cache
                    $arrayCachePId = $cache [$_SESSION ['UId']];

                    if (array_key_exists($pid, $arrayCachePId)) {

                        $this->updateCache($pid);
                        $this->updateUserProduct($pid);
                    } 
                    
                    else {

                        $this->insertCache($pid);
                        $this->insertUserProduct($pid);
                    }
                } 
                
                else {

                    //添加cache
                    $this->insertCache($pid);
                    $this->insertUserProduct($pid);
                }
            } 
            
            else {
                
                $cache = array ( $_SESSION [self::SESSION_ID ] => array ( $pid => array ( 'Count' => 1 , 'UpdateTime' => date ( 'Y-m-s H:m:s' ) ) ) ) ;
                $this->addCache($cache) ;
            }
        }
    }

    
    
    /*
     * 数据挖掘
     * 用户查询记录存入cache
     */

    public function saveToCollect($productInfo) {

        /*
         * 判断有无
         * 数据挖掘cache
         */
        
        $pid = $productInfo ['PId'] ;
        echo $pid;
        
        $cacheCollect = $this->getCacheValue(self::CACHE_COLLECT_NS);
        print_r($cacheCollect);
        
        if ($cacheCollect) {

            $cacheCollect [ date ('Y-m-d H:m:s', time() ) ] = array ( $pid => array ( 'UId' => $_SESSION[self::SESSION_ID], 'Date' =>1) );
            $this->_cacheInstance->set(self::CACHE_COLLECT_NS, $cacheCollect);
        } 
        
        else {

            $db = $this->_dbInstance;
            $row = $db->getList(self::TABLE_NAME, '1');

            if (count($row) > 0) {

                foreach ($row as $value) {

                    $collect [$value ['UpdateTime']] = array($value ['PId'] => array('UId' => $value ['UId'], 'Count' => $value ['Count']));
                }

                $this->_cacheInstance->add(self::CACHE_COLLECT_NS, $collect);
            } 
            
            else {
                
                $cache = array ( date ( 'Y-m-d H:m:s' , time() ) ) ;
                $this-> addCache ( $cache ) ;
            }
        }
    }

    public function __destruct() {
        
    }
    public function getUserProuct(){
        
        $list = common::getMysql()->getList(self::TABLE_NAME_SEARCH , 1 );
        return $list;
    }

}

?>








